class Solution 
{
public:
    int maxTurbulenceSize(vector<int>& arr) 
    {
        int n=arr.size();
        vector<int>f(n,0);
        auto g=f;
        f[0]=g[0]=1;
        int ret=1;
        for(int i=1;i<n;i++)
        {
            if(arr[i-1]<arr[i])
            {
                f[i]=g[i-1]+1;
                g[i]=1;
            }
            else if(arr[i-1]>arr[i])
            {
                g[i]=f[i-1]+1;
                f[i]=1;
            }
            else
            {
                f[i]=g[i]=1;
            }
            ret=max(ret,max(f[i],g[i]));
        }

        return ret;
    }
};